package com.janj.bean;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

//测试类:添加一条记录
public class Test3 {
    public static void main(String[] args) throws Exception {

        //1.加载配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        //2.获取工厂对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //3.获取对象
        SqlSession session = factory.openSession();

        //4.执行SQL语句 ,找到 Mapper 对应的id
        Student stu = new Student(null, "jane", 30);
        int lineNumber = session.insert("StudentMapper.insertOne", stu);

        //5.提交事务
        session.commit();

        //6.处理结果
        System.out.println("lineNumber = " + lineNumber);
        System.out.println(lineNumber > 0 ? "成功" : "失败");

        //7.释放资源
        session.close();
        is.close();

    }
}
